package com.xjk.xml;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class XmlUtil{

    /**
     * 利用JAXB将对象解析成XML
     */
    public static String buildJaxb(Object object) throws Exception{
        return buildJaxb(object, Boolean.TRUE, null);
    }

    /**
     * 利用JAXB将对象解析成XML
     */
    public static String buildJaxb(Object object,Boolean format,String namespacePrefix) throws Exception{
        String result = null;
        Class<?> clazz = object.getClass();
        JAXBContext jc = JAXBContext.newInstance(clazz);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, format);
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        //        marshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        if (null == namespacePrefix){
            //            marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefix(namespacePrefix));
        }
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        marshaller.marshal(object, os);
        result = new String(os.toByteArray(), "UTF-8");

        return result;
    }

    /**
     * 利用JAXB将XML解析成对象
     */
    @SuppressWarnings("unchecked")
    public static <T> T buildJaxb(Class<T> clazz,String xml) throws Exception{
        T object = null;
        Object o = null;
        try{
            JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            o = unmarshaller.unmarshal(new StringReader(xml));
            if (o instanceof JAXBElement){
                JAXBElement<?> element = (JAXBElement<?>) o;
                object = (T) element.getValue();
            }else{
                object = (T) unmarshaller.unmarshal(new StringReader(xml));
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
        return object;
    }

    /**
     * 利用JAXB将XML解析成对象
     */
    @SuppressWarnings("unchecked")
    public static <T> T buildJaxb(Class<T> clazz,File file) throws Exception{
        T object = null;
        try{
            JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            Object object1 = unmarshaller.unmarshal(file);

            if (object1 instanceof JAXBElement){
                JAXBElement<?> element = (JAXBElement<?>) object1;
                object = (T) element.getValue();
            }else{
                object = (T) unmarshaller.unmarshal(file);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
        return object;
    }
}
